//
//  LSPagedView.swift
//  LiveShow
//
//  Created by dai zhong on 2017/4/28.
//  Copyright © 2017年 company.cn. All rights reserved.
//

import UIKit

class LSPagedView: UIView {

    fileprivate lazy var titleView :LSTitleView = {
    
        let titleView = LSTitleView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.style.height), titles: self.titles as [String], style: self.style);
        titleView.delegate = self;
        return titleView;
    }()
    fileprivate lazy var pagedCollectionView:PagedCollectionView = {
    
        let pagedCollectionView = PagedCollectionView(pagedCollectionFrame: CGRect(x: 0, y: self.style.height, width: self.frame.width, height: self.frame.height - self.style.height), pagedCollectionStyle: self.pagedCollectionStyle);
        pagedCollectionView.dataSource = self;
        pagedCollectionView.delegate = self;
        return pagedCollectionView;
    }()
    
    fileprivate var titles:[NSString]!;
    fileprivate var style :LSTitleSyle!;
    fileprivate var childVcs:[UIViewController]!;
    fileprivate weak var parentVc:UIViewController!
    fileprivate lazy var pagedCollectionStyle:PagedCollectionStyle = {
    
        var pagedCollectionStyle = PagedCollectionStyle();
        pagedCollectionStyle.isShowPageControl = false
        pagedCollectionStyle.aPageRowCout = 1;
        pagedCollectionStyle.aPageColumnCount = 1;
        pagedCollectionStyle.edgInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0);
        pagedCollectionStyle.lineSpacing = 0.0;
        pagedCollectionStyle.interitemSpacing = 0.0;
        return pagedCollectionStyle;
    }()
    
    convenience init(frame: CGRect, titles: [String], style: LSTitleSyle, childVcs: [UIViewController], parentVc: UIViewController){
    
        self.init(frame:frame);
        
        self.titles = titles as [NSString]!;
        self.style = style;
        self.childVcs = childVcs;
        self.parentVc = parentVc;
        
        setupUI();
    }
}

// MARK:- setupUI
extension LSPagedView{


    fileprivate func setupUI(){
    
        self.addSubview(titleView);
        
        self.addSubview(pagedCollectionView);
    }
}

// MARK:- LSTitleViewProtocol
extension LSPagedView:LSTitleViewProtocol{

    func titleView(_ titleView: LSTitleView, didSelectItemAt index: NSInteger){
    
        print("didSelectItemAt index === \(index)" );
    }
}

// MARK:- PagedCollectionViewDataSource
extension LSPagedView:PagedCollectionViewDataSource{

    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.childVcs.count
    }
    
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    
        let cell = pagedCollectionView.dequeueReusableCell(withReuseIdentifier: kPagedCollectionCellIdentifier, for: indexPath);
        
        for subview in cell.contentView.subviews {
            subview.removeFromSuperview()
        }
        let vcView = self.childVcs[indexPath.item].view as UIView;
        vcView.frame = cell.contentView.bounds;
        let color = UIColor.randomColor
        print(color)
        vcView.backgroundColor = color
        cell.contentView.addSubview(vcView)
        return cell;
    }
}

// MARK:- PagedCollectionViewDelegate
extension LSPagedView:PagedCollectionViewDelegate{

    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didSelectItemAt indexPath: IndexPath){
         print("didSelectItemAt indexPath: \(indexPath)")
    }
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didScrolledPageAt indexPath: IndexPath){
        print("didScrolledPageAt indexPath: \(indexPath)")
    }
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView){
        print("pagedCollectionView")
    }
}






